/*
 * Copyright 2011 evswork Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.evswork.getwit;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.evswork.getwit.smackwrap.Talk;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

public class UserOperationDlgWrapper extends AlertDialog.Builder implements OnClickListener {
	GttMainActivity act;
	TweetObject data;
	String user;
	
	public static String getUser(String rawdata) {
		Pattern pattern = Pattern.compile(
				"(?:用户个人信息\n\n用户名： |User Profile:\n\nUsername: )([_0-9a-zA-Z]+)\n\n",
				Pattern.DOTALL);
		Matcher matcher = pattern.matcher(rawdata);
		if (matcher.find()) {
			return matcher.group(1);
		} else {
			return null;
		}
	}
	
	private void buildDialog() {
		this
			.setTitle(String.format("用户:%s", user))
			.setItems(
				act.getResources().getStringArray(R.array.user_op_dlg),this)
			.show();
	}
	
	UserOperationDlgWrapper(GttMainActivity act,TweetObject data) {
		super(act);
		this.act = act;
		this.data = data;
		this.user = getUser(data.content);
		buildDialog();
	}

	private String cmd;
	private void ensureOperation(String prefix){
		cmd = String.format("%s %s", prefix,user);
		new AlertDialog.Builder(act)
			.setTitle(String.format("确认发送 \"%s\"?", cmd))
			.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					try {
						Talk.send(cmd);
					} catch (Exception e) {
						//Log.e("Getwit",e.toString());
					}
				}
			})
			.setNegativeButton("取消", null)
			.show();
		
	}
	
	@Override
	public void onClick(DialogInterface dialog, int which) {
		switch (which) {
		case 0: // tl
			act.pctlWrapper.show(String.format("tl %s",user), true);
			break;
		case 1: // reply
			act.edWrapper.RefView.update(user, RefTweetView.MODE_RPY_USR);
			act.showEditLayout(EditPageWrapper.FROM_MAIN, true);
			//act.edWrapper.setMsg(String.format("@%s %s",user,act.edWrapper.getMsg()));
			break;
		case 2: // dm
			act.edWrapper.RefView.update(user, RefTweetView.MODE_DM_USR);
			act.showEditLayout(EditPageWrapper.FROM_MAIN, true);
			//act.edWrapper.setMsg(String.format("-d %s %s",user,act.edWrapper.getMsg()));
			break;
		case 3: // fo
			ensureOperation("-fo");
			break;
		case 4: // uf
			ensureOperation("-uf");
			break;
		case 5: // block
			ensureOperation("-block");
			break;
		case 6: // if
			Talk.send(String.format("-if %s",user));
			break;
		}
		
		dialog.dismiss();
	}
}
